package com.example.androidautoproject.utils

import android.annotation.SuppressLint
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import com.blankj.utilcode.util.ScreenUtils
import com.example.androidautoproject.MainActivity
import com.example.androidautoproject.databinding.OverlayStartBinding
import com.ven.assists.service.AssistsService
import com.ven.assists.service.AssistsServiceListener
import com.ven.assists.window.AssistsWindowManager

@SuppressLint("StaticFieldLeak")
object OverlayStart : AssistsServiceListener {

    private fun requestMediaProjectionPermission () {
        AdParam.screenPackageName = AdParam.packageName
        // 启动一个 Activity 来请求权限
        val intent = Intent(AssistsService.instance, MainActivity::class.java).apply {
            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) // 因为可能从服务上下文启动，需要此 flag
            // 可以putExtra一个特定的action，告诉MainActivity是来请求权限的
            putExtra("REQUEST_MEDIA_PROJECTION", true)
        }
        AssistsService.instance?.startActivity(intent)
    }

    private var viewBinding: OverlayStartBinding? = null
        @SuppressLint("ClickableViewAccessibility")
        get() {
            if (field == null) {
                field = OverlayStartBinding.inflate(LayoutInflater.from(AssistsService.instance)).apply {
                    btnStop.setText("开始${AdParam.ip}")
                    btnStop.setOnClickListener {
                        if(!Overlay.showed){
                            Overlay.show()
                        }
                    }
//                    btnCaptureScreen.setOnClickListener{
//                        requestMediaProjectionPermission()
//                    }
                }
            }
            return field
        }

    var onClose: ((parent: View) -> Unit)? = null

    var showed = false
        private set
        get() {
            assistWindowWrapper?.let {
                return AssistsWindowManager.isVisible(it.getView())
            } ?: return false
        }

    var assistWindowWrapper: AssistsWindowWrapperCustom? = null
        private set
        get() {
            viewBinding?.let {
                if (field == null) {
                    field = AssistsWindowWrapperCustom(it.root, wmLayoutParams = AssistsWindowManager.createLayoutParams().apply {
                        width = (ScreenUtils.getScreenWidth() * 0.2).toInt()
                        height = (ScreenUtils.getScreenHeight() * 0.1).toInt()
                    }, onClose = { hide() }).apply {
                        minWidth = (ScreenUtils.getScreenWidth() * 0.2).toInt()
                        minHeight = (ScreenUtils.getScreenHeight() * 0.1).toInt()
                        initialCenter = false
                        showBackground = false
                        showOption = false
                        viewBinding.tvTitle.text = ""
                    }
                }
            }
            return field
        }

    fun show() {
        if (!AssistsService.listeners.contains(this)) {
            AssistsService.listeners.add(this)
        }
        if (!AssistsWindowManager.contains(assistWindowWrapper?.getView())) {
            AssistsWindowManager.add(assistWindowWrapper)
        }
    }

    fun hide() {
        AssistsWindowManager.removeView(assistWindowWrapper?.getView())
    }

    override fun onUnbind() {
        viewBinding = null
        assistWindowWrapper = null
    }
}

private fun AssistsWindowManager.add(windowWrapper: AssistsWindowWrapperCustom?, isStack: Boolean = true, isTouchable: Boolean = true) {
    windowWrapper ?: return
    AssistsWindowManager.add(
        view = windowWrapper.getView(),
        layoutParams = windowWrapper.wmlp,
        isStack = isStack,
        isTouchable = isTouchable
    )
}

